<?php

namespace addons\comments\controller;


use addons\comments\model\CommentsPostModel;
use app\common\traits\ControllerCurd;
use think\Request;

class AdminIndex extends Base
{
    use ControllerCurd;
    public $model ;

    public function initialize()
    {
        parent::initialize();

        $this->model = new CommentsPostModel();
    }

    public function index(Request $request)
    {
        $topic_type = $request->param("topic_type",1,'intval');

        $where[] = ['topic_type','eq',$topic_type];

        $keywords = $this->request->param("keywords");

        if($keywords) array_push($where,["content","like","%{$keywords}%"]);

        $with = "";

        switch ($topic_type)
        {
            case 1:
                $with = "article";
                break;
        }
        $dataList = $this->model->where($where)
            ->with($with)
            ->order("create_time desc")
            ->paginate();


        $this->assign([
            "dataList" => $dataList,
            "topic_type"=> $topic_type,
            "search"   => [
                "keywords" => $keywords,
            ]
        ]);

        return $this->fetch();
    }

    public function delete()
    {
        $this->del(addons_url("comments://admin_index/index"));
    }

    public function read()
    {
        $id = $this->request->param("id",0,'intval');
        $topic_type = $this->request->param("topic_type",1,'intval');
        $where[] = ['topic_type','eq',$topic_type];
        $with = "";
        switch ($topic_type)
        {
            case 1:
                $with = "article,user";
                break;
        }
        $data = $this->model->where("id",$id)
            ->where($where)
            ->with($with)
            ->find();

        $this->assign([
            "data" => $data
        ]);

        return $this->fetch();

    }

    public function save()
    {
        $this->edit(addons_url("comments://admin_index/index"));
    }

    public function create()
    {
        $this->add(addons_url("comments://admin_index/index"));
    }
}